package com.py.credit.service.impl;

import com.py.credit.common.Page;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.dao.CswhDao;
import com.py.credit.dao.LyDao;
import com.py.credit.entity.CswhObj;
import com.py.credit.entity.TreeObj;
import com.py.credit.service.CswhService;
import com.py.credit.utils.DeptTreeUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class CswhServiceImpl implements CswhService {

    @Autowired
    private CswhDao cswhDao;

    @Autowired
    private LyDao lyDao;

    @Autowired
    private DeptTreeUtil deptTreeUtil;

    @Override
    public List<Map<String, String>> selectDeptList(String code, String name, Page page) {
        return cswhDao.selectDeptListForPage(code, name, page);
    }

    @Override
    public List<TreeObj> selectDeptTreeList(String name) {
        return cswhDao.selectDeptTreeList(name);
    }

    @Override
    public List<TreeObj> selectLyTreeList(String name, String type) {
        // TODO Auto-generated method stub
        return cswhDao.selectLyTreeList(name, type);
    }

    @Override
    public List<CswhObj> getListForPage(String id, String jclb, String csbm, String remark, String deptId, String lyId, Page page) {
        return cswhDao.getListForPage(id, jclb, csbm, remark, deptId, lyId, page);
    }

    @Override
    public void add(String id, String jclb, String cs, String remark, String yj, String jczt, String csbm, String sort, String csType, String cssdValue) {
        cswhDao.add(id, jclb, cs, remark, yj, jczt, csbm, Integer.parseInt(sort), csType, cssdValue);
    }

    @Override
    public void modify(String id, String cs, String remark, String yj, String jczt, String csbm, String sort, String cssdValue) {
        cswhDao.modify(id, cs, remark, yj, jczt, csbm, Integer.parseInt(sort), cssdValue);
    }

    @Override
    public RestServiceResult del(String type, String ids) {
        if (StringUtils.isBlank(ids) || ids.split(",").length <= 0) {
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "请选择行进行删除");
        }

        int count = cswhDao.selectLyCsSameById(type, null, null,Arrays.asList(ids.split(",")),"P");
        if (count > 0) {
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "该措施已被部门或分类使用");
        }

        count = cswhDao.selectDeptCsSameById(type, null, null, Arrays.asList(ids.split(",")),"P");
        if (count > 0) {
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "该措施已被部门或分类使用");
        }

        cswhDao.del(Arrays.asList(ids.split(",")), "Y");
        return new RestServiceResult(RestServiceResult.CODE_SUCCESS);
    }

    @Override
    public int selectSameCode(String csbm, String remark) {
        return cswhDao.selectSameCode(csbm, remark);
    }

    @Override
    public int selectOtherSameCode(String id, String csbm, String remark) {
        return cswhDao.selectOtherSameCode(id, csbm, remark);
    }

    @Override
    public RestServiceResult addCsLy(String type, String lyId, String csIdList) {
        List<String> list = Arrays.asList(csIdList.split(","));
        int count = cswhDao.selectLyCsSameById(type, lyId, null, list,null);
        if (count > 0) {
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "措施已在该分类下存在!");
        }

        for (String csId : list
                ) {
            cswhDao.insertCsLy(lyId, csId);
        }

        return new RestServiceResult(RestServiceResult.CODE_SUCCESS);
    }

    @Override
    public void delCsLy(String ly, String cs) {
        cswhDao.delCsLy(ly, Arrays.asList(cs.split(",")));
    }

    @Override
    public RestServiceResult addCsDept(String type, String deptId, String csIdList) {
        List<String> list = Arrays.asList(csIdList.split(","));
        int count = cswhDao.selectDeptCsSameById(type, deptId, null, list,null);
        if (count > 0) {
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "措施已在该部门下存在!");
        }

        for (String csId : list
                ) {
            cswhDao.insertCsDept(deptId, csId);
        }
        return new RestServiceResult(RestServiceResult.CODE_SUCCESS);
    }

    @Override
    public void delCsDept(String dept, String cs) {
        cswhDao.delCsDept(dept, Arrays.asList(cs.split(",")));
    }

    @Override
    public List<CswhObj> selectByGzId(String id) {
        // TODO Auto-generated method stub
        return cswhDao.selectByGzId(id);
    }

    @Override
    public CswhObj selectOneById(String id) {
        // TODO Auto-generated method stub
        return cswhDao.selectOneById(id);
    }

    @Override
    public void excelExport(HttpServletResponse response, String type) {
        queryTableExecle(response, type);
    }

    private void queryTableExecle(HttpServletResponse response, String type) {
        // 生成提示信息
        response.setContentType("application/vnd.ms-excel");
        HSSFWorkbook workbook = null;
        OutputStream fOut = null;
        String codedFileName = "部门措施数据";
        // 进行转码，使其支持中文文件名
        try {
            codedFileName = URLEncoder.encode(codedFileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
        // 产生工作簿对象
        workbook = new HSSFWorkbook();
        // 产生工作表对象
        HSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(0, "具体部门措施数据");
        HSSFRow row = null;
        HSSFCell cell = null;

        String[] strBT = {"部门名称", "措施编码", "惩戒措施", "适用主体", "措施手段", "法律及政策依据", "奖惩类别"};
        List<TreeObj> list = deptTreeUtil.getTree(lyDao.selectDeptTreeList(type, null));

        
        //创建标题行
        row = sheet.createRow(0);
        for (int i = 0; i < strBT.length; i++) {
            cell = row.createCell(i);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(strBT[i]);
        }

        int count = 1;
        for (int i = 0; i < list.size(); i++) {
            List<CswhObj> cswhObjList = cswhDao.selectCswhByDeptName(type, list.get(i).getName());

            for (int j = 0; j < cswhObjList.size(); j++) {
                row = sheet.createRow(count);//创建一行
                cell = row.createCell(0);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue((String) list.get(i).getName());

                cell = row.createCell(1);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue((String) cswhObjList.get(j).getCsbm());

                cell = row.createCell(2);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue((String) cswhObjList.get(j).getRemark());

                cell = row.createCell(3);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                if (cswhObjList.get(j).getJczt().equals("C")) {
                    cell.setCellValue("法人");
                } else if (cswhObjList.get(j).getJczt().equals("P")) {
                    cell.setCellValue("自然人");
                } else {
                    cell.setCellValue("所有");
                }

                cell = row.createCell(4);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(cswhObjList.get(j).getCssdValue());

                cell = row.createCell(5);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(cswhObjList.get(j).getYj());

                cell = row.createCell(6);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                if (cswhObjList.get(j).getJclb().equals("C")) {
                    cell.setCellValue("处罚");
                } else {
                    cell.setCellValue("激励");
                }

                count++;
            }
        }

        list = null;

        try {
            fOut = response.getOutputStream();
            workbook.write(fOut);
            workbook.close();
            fOut.flush();
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int selectBdGzCs(String id) {
        return cswhDao.selectBdGzCs(id);
    }
}
